// FuncParaDlg.cpp : implementation file
/*********************************************************************
*	Author         : orbit
*	Date           : November 22, 2000
* Last-modified  : June 4, 2002 
*	Contact us     : inte2000@163.com,support@winmsg.com
*	Web Page       : http://www.winmsg.com/cn/orbit.htm (for Chinese version)
*						       http://www.winmsg.com/orbit.htm (for English version)
**********************************************************************/

#include "stdafx.h"
#include "tabbars.h"
#include "FuncParaDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CFuncParaDlg dialog


//##ModelId=431C34CB004E
CFuncParaDlg::CFuncParaDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFuncParaDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFuncParaDlg)
	//}}AFX_DATA_INIT
}


//##ModelId=431C34CB009E
void CFuncParaDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFuncParaDlg)
	//}}AFX_DATA_MAP
	DDX_GridControl(pDX, IDC_GRID, m_Grid);
}


BEGIN_MESSAGE_MAP(CFuncParaDlg, CDialog)
	//{{AFX_MSG_MAP(CFuncParaDlg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFuncParaDlg message handlers
//##ModelId=431C34CB00BB
BOOL CFuncParaDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	m_listImage.Create(16,16,ILC_COLOR8|ILC_MASK,2,1);
	m_listImage.Add(::LoadIcon(AfxGetInstanceHandle(),(LPCTSTR)IDI_ICON_POINT));
	m_listImage.Add(::LoadIcon(AfxGetInstanceHandle(),(LPCTSTR)IDI_ICON_AND));
	m_listImage.Add(::LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_ICON_FULL)));
	m_listImage.Add(::LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDI_ICON_RETURN)));

	CString strUserName;
	CIni ini(g_szIniPathName);
	ini.GetValue(lpszGeneral,lpszUser,strUserName,lpszNull);
	ini.Clear();
	SetDlgItemText(IDC_STATIC_FUNCNAME,m_strFuncName);
	SetDlgItemText(IDC_STATIC_FUNCTYPE,m_strRetType);
	SetDlgItemText(IDC_EDIT_NAME,strUserName);

	int size = m_strAPapaList.GetSize();
	try 
	{
		m_Grid.SetRowCount(size + 2);
		m_Grid.SetColumnCount(3);
		m_Grid.SetFixedRowCount(1);
		m_Grid.SetFixedColumnCount(1);
	}
	catch (CMemoryException* e)
	{
		e->ReportError();
		e->Delete();
		return FALSE;
	}

	m_Grid.ModifyStyle(WS_BORDER,0);
	m_Grid.ModifyStyleEx(WS_EX_CLIENTEDGE,WS_EX_STATICEDGE);
	m_Grid.SetImageList(&m_listImage);

	GV_ITEM Item;
	Item.mask = GVIF_TEXT|GVIF_FORMAT;
	Item.row = 0;
	Item.col = 0;
	Item.nFormat = DT_LEFT|DT_WORDBREAK;
	Item.szText = _T("Parameter Transfer");
	m_Grid.SetItem(&Item);
	
	Item.col = 1;
	Item.szText = _T("Parameter declare");
	m_Grid.SetItem(&Item);

	Item.col = 2;
	Item.szText = _T("Parameter explain");
	m_Grid.SetItem(&Item);
	
	for(int i = 0; i < size + 1; i++)
	{
		int type;
		CString strTmp;
		if(i == size)
			strTmp = m_strRetType;
		else
			strTmp = m_strAPapaList[i];
		int idxStar = strTmp.Find(_T('*'),0);
		int idxAnd = strTmp.Find(_T('&'),0);
		if(idxStar >= 0 && idxAnd < 0)
			type = 0;
		else if(idxStar < 0 && idxAnd >= 0)
			type = 1;
		else if(idxStar >= 0 && idxAnd >= 0)
			type = idxStar > idxAnd ? 0 : 1;
		else
		{
			strTmp.TrimLeft();
			if(strTmp.Left(2) == _T("LP") || strTmp.Left(2) == _T("lp"))
				type = 0;
			else
				type = 2;
		}
		for (int col = 0; col < m_Grid.GetColumnCount(); col++)
		{ 
			GV_ITEM Item;
			Item.mask = GVIF_TEXT|GVIF_FORMAT;
			Item.row = i + 1;
			Item.col = col;
			if(col == 0) 
			{
				Item.nFormat = DT_RIGHT|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS;
				if(i == size)
					Item.szText = _T("Retun Value");
				else
					Item.szText = (type == 0 ?_T("Pointer") : (type == 1 ? _T("Reference") : _T("By Value")) );
			} 
			else if(col == 1)
			{
				Item.nFormat = DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS;
				strTmp.Replace(_T("&"),_T("&&"));
				Item.szText = strTmp;
			}
			else
			{
				Item.nFormat = DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS;
				Item.szText = lpszNull;
			}
			m_Grid.SetItem(&Item);
//			m_Grid.SetItemBkColour(row, col, clr);
//			m_Grid.SetItemFgColour(row, col, RGB(255,0,0));
		}
		m_Grid.SetItemBkColour(i + 1, 0, RGB(0,128,192));
		if(i == size)
			m_Grid.SetItemImage(i + 1,0,3);
		else
			m_Grid.SetItemImage(i + 1,0,type);
		m_Grid.SetRowHeight(i + 1, 2*m_Grid.GetRowHeight(i + 1)/3);
	  m_Grid.SetItemState(i + 1,0, m_Grid.GetItemState(i+1,0) | GVIS_READONLY);
	  m_Grid.SetItemState(i + 1,1, m_Grid.GetItemState(i+1,1) | GVIS_READONLY);
	}

	m_Grid.SetHeaderSort(FALSE);
	m_Grid.SetEditable(TRUE);
	m_Grid.SetListMode(TRUE);
	m_Grid.EnableDragAndDrop(TRUE);
	m_Grid.SetTextBkColor(RGB(0xFF, 0xFF, 0xE0));
  m_Grid.EnableTitleTips(TRUE);
	m_Grid.AutoSize();
	m_Grid.SetColumnWidth(2,200);
	m_Grid.SetRowHeight(0, 2*m_Grid.GetRowHeight(0)/3);
//	m_Grid.SetRowHeight(0, 3*m_Grid.GetRowHeight(0)/2);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

//##ModelId=431C34CB007D
void CFuncParaDlg::SetPara(CString & strFuncName,CStringArray & para,CString & strRetType)
{
	m_strFuncName = strFuncName;
	m_strAPapaList.Copy(para);
	m_strRetType = strRetType;
}

//##ModelId=431C34CB009C
BOOL CFuncParaDlg::DestroyWindow() 
{
	m_Grid.SetImageList(NULL);
	m_Grid.DeleteAllItems();
	m_Grid.DeleteColumn(0);
	m_Grid.DeleteColumn(1);
	m_Grid.DeleteColumn(2);
	m_listImage.DeleteImageList();
	m_strAPapaList.RemoveAll();
	
	return CDialog::DestroyWindow();
}


//##ModelId=431C34CB00BD
void CFuncParaDlg::OnOK() 
{
	GetDlgItemText(IDC_EDIT_NAME,m_strUserName);
	GetDlgItemText(IDC_EDIT_FUNCPLARE,m_strFuncDesc);
	int count = m_Grid.GetRowCount() - 1;
	for(int i = 0; i < count; i++)
	{
		CString strTmp = m_Grid.GetItemText(i + 1,2);
		m_strARtn.Add(strTmp);
	}
	CDialog::OnOK();
}
